package com.rzjm.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.*;
import com.rzjm.api.entity.*;
import com.rzjm.api.service.TerminalFittingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TerminalFittingServiceImpl extends BaseServiceImpl<TerminalFittingDao, TerminalFitting>
        implements TerminalFittingService {


    @Resource
    private CkTerminalDao ckTerminalDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysDictDao sysDictDao;
    @Resource
    private ShopDao shopDao;
    @Resource
    private TerminalFittingDao terminalFittingDao;
    @Resource
    private TerminalCheckRecordDao terminalCheckRecordDao;
    @Resource
    private TerminalCheckRecordImgDao terminalCheckRecordImgDao;

    @Value("${web.imgprefix}")
    private String imgprefix;//图片地址

    @Override
    public List<TerminalFitting> getTerminalFittingList(User user, Integer health, String keyword, Integer offset, Integer pageSize) {
        Map<String,Object> map  = new HashMap<>();
        SysUser sysUser = sysUserDao.selectById(user.getSysUserId());
        if(sysUser != null){
            if(sysUser.getUserType() >= 4){
                map.put("shop_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 2){
                map.put("jituan_user_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 3){
                map.put("sys_user_id",sysUser.getShopId());
            }
        }
        map.put("health",health);
        if(StrUtil.isNotEmpty(keyword)){
            map.put("keyword",keyword);
        }
        List<TerminalFitting> list = terminalFittingDao.getPageByUserId(map,offset,pageSize);
        list.forEach(fitting -> {
            CkTerminal terminal = ckTerminalDao.selectById(fitting.getTerminalId());
            if(terminal!=null){
                Shop shop = shopDao.selectById(terminal.getShopId());
                if(shop!=null){
                    terminal.setShopName(shop.getShopName());
                }
            }
            fitting.setTerminal(terminal);
            QueryWrapper<SysDict> qw2 = new QueryWrapper<>();
            qw2.eq("type", "clean-cycle-type");
            qw2.eq("value", fitting.getHealthCountUnit());
            SysDict dict = sysDictDao.selectOne(qw2);
            if(dict!=null){
                fitting.setHealthCycle("1次/" + fitting.getHealthCount() + dict.getName());
            }

            QueryWrapper<TerminalCheckRecord> qw = new QueryWrapper<>();
            qw.eq("fitting_id",fitting.getId());
            qw.orderByDesc("create_time");
            qw.last(" LIMIT 1");
            TerminalCheckRecord record = terminalCheckRecordDao.selectOne(qw);
            if(record!=null){
                List<TerminalCheckRecordImg> imgs = terminalCheckRecordImgDao.selectListByColumn("record_id",record.getId());
                if(imgs!=null){
                    imgs.forEach(img->{
                        img.setImgUrl(imgprefix +img.getImgUrl());
                    });
                }
                record.setImgs(imgs);
            }
            fitting.setLastRecord(record);
        });
        return list;
    }

    @Override
    public List<Map<String,Object>> getWarningPageByUserId(int warning_day) {
        Map<String,Object> map = new HashMap<>();
        map.put("warning_day",warning_day);
        return terminalFittingDao.getWarningPageByUserId(map);
    }

    @Override
    public List<TerminalFitting> getWarningFittingPageByUserId(User user, Integer offset, Integer pageSize) {
        Map<String,Object> map  = new HashMap<>();
        SysUser sysUser = sysUserDao.selectById(user.getSysUserId());
        if(sysUser != null){
            if(sysUser.getUserType() >= 4){
                map.put("shop_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 2){
                map.put("jituan_user_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 3){
                map.put("sys_user_id",sysUser.getShopId());
            }
        }
        map.put("warning_day",7);
        List<TerminalFitting> list = terminalFittingDao.getWarningFittingPageByUserId(map,offset,pageSize);
        if(list!=null){
            list.forEach(fitting -> {
                CkTerminal terminal = ckTerminalDao.selectById(fitting.getTerminalId());
                if(terminal!=null){
                    Shop shop = shopDao.selectById(terminal.getShopId());
                    if(shop!=null){
                        terminal.setShopName(shop.getShopName());
                    }
                }
                fitting.setTerminal(terminal);
                QueryWrapper<SysDict> qw2 = new QueryWrapper<>();
                qw2.eq("type", "clean-cycle-type");
                qw2.eq("value", fitting.getHealthCountUnit());
                SysDict dict = sysDictDao.selectOne(qw2);
                if (dict != null) {
                    fitting.setHealthCycle("1次/" + fitting.getHealthCount() + dict.getName());
                }
            });
        }
        return list;
    }
}
